package com.taplinker.client.im.rpc;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import com.taplinker.client.App;
import com.taplinker.client.im.domain.ImDataCenter;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.rpc.body.Cmd104Req;
import com.taplinker.client.im.rpc.body.CustomerBody;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.util.json.JsonUtil;

public class Cmd104 extends SimpleSocketCommand<Cmd104Req> {

	public Cmd104(String source, int recordNo, String order, int pageSize, String type) {
		body = new Cmd104Req();
		body.setOrder(order);
		body.setPageSize(pageSize);
		body.setRecordNo(recordNo);
		body.setSource(source);
		body.setType(type);
	}
	
	public Cmd104(int recordNo, int pageSize, String type) {
		body = new Cmd104Req();
		body.setOrder("desc");
		body.setPageSize(pageSize);
		body.setRecordNo(recordNo);
		body.setType(type);
	}
	
	public Cmd104(String source, boolean more, String type) {
		if(!more) {
			ImDataCenter.getInstance().getPubQueue().clear();
		}
		body = new Cmd104Req();
		body.setOrder("desc");
		body.setPageSize(ImDataCenter.PAGE_SIZE);
		if(ImDataCenter.getInstance().isPublicQueue()) {
			body.setRecordNo(ImDataCenter.getInstance().getPubQueue().size());
		}else{
			body.setRecordNo(ImDataCenter.getInstance().getPrivQueue().size());
		}
		body.setType(type);
		if(!TextUtils.isEmpty(source)) {
			body.setSource(source);
		}
	}
	
	public Cmd104(String source, boolean more, String type, String sdkAccount, int all) {
		if(!more) {
			ImDataCenter.getInstance().getPubQueue().clear();
		}
		body = new Cmd104Req();
		body.setOrder("desc");
		body.setPageSize(ImDataCenter.PAGE_SIZE);
		body.setRecordNo(ImDataCenter.getInstance().getPubQueue().size());
		body.setType(type);
		if(!TextUtils.isEmpty(source)) {
			body.setSource(source);
		}
		body.setSdkAccount(sdkAccount);
		body.setAll(all);
	}
	
	public Cmd104(String source, String type) {
		this(source, false, type);
	}
	
	@Override
	public void yes(Result result) {
		super.yes(result);
//		Log.d("log_ktian", "receive Body："+((IMMessageResponse) result.object).getMessage().getBody());
//		List<Cmd004Rep> list = JsonUtil.jsonToList(((IMMessageResponse) result.object).getMessage().getBody(), Cmd004Rep.class);
//		List<TapLinkerConversation> queue = new ArrayList<TapLinkerConversation>();
//		for(Cmd004Rep value : list) {
//			TapLinkerConversation conversation = ConvertUtil.convertToConversation(value);
//			conversation.setQueueType(TapLinkerConversation.PUBLIC_QUEUE);
//			queue.add(conversation);
//		}
//		ImDataCenter.getInstance().addAllPublicQueue(queue);
//		if(list == null || list.size() < 1) {
//			ImDataCenter.getInstance().noticeListener(ImDataCenter.CONVERSATION_NO_MORE);
//		}
		
		try {
//			Log.v("log_ktian", "customer:"+new JSONObject( ((IMMessageResponse) result.object).getMessage().getBody() ).getString("customer") );
//			Log.v("log_ktian", "conversation:"+new JSONObject( ((IMMessageResponse) result.object).getMessage().getBody() ).getString("conversation") );
			List<TapLinkerConversation> conversationList = JsonUtil.jsonToList(new JSONObject( ((IMMessageResponse) result.object).getMessage().getBody() ).getString("conversation"), TapLinkerConversation.class);
			List<CustomerBody> customerBodyList = JsonUtil.jsonToList(new JSONObject( ((IMMessageResponse) result.object).getMessage().getBody() ).getString("customer"), CustomerBody.class);
			
			// 融合用户头像信息
			for (CustomerBody customerBody : customerBodyList) {
				String name = customerBody.getName();
				for(int i=0; i<conversationList.size(); i++) {
					if( name.equals(conversationList.get(i).getSdkAccount()) ) {
						conversationList.get(i).setCustomerIcon(customerBody.getIcon());
						String nickName = customerBody.getSdkName();
						if(nickName!=null && !"".equals(nickName.trim())) {
							conversationList.get(i).setNickName(nickName);
						}
//						Log.v("log_ktian", customerBody.getName()+"用户头像000:"+customerBody.getIcon());
//						Log.v("log_ktian", "用户头像111:"+conversationList.get(i).getCustomerIcon());
					}
				}
			}
			if(ImDataCenter.getInstance().isPublicQueue()) {
				ImDataCenter.getInstance().addAllPublicQueue(conversationList);
			}else{
				ImDataCenter.getInstance().addAllPrivateQueue(conversationList);
			}
			if(conversationList == null || conversationList.size() < 1) {
				ImDataCenter.getInstance().noticeListener(ImDataCenter.CONVERSATION_NO_MORE);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
	
	@Override
	public void no(Result result) {
		super.no(result);
		if(result.object!=null) {
			// 需要重新登陆
			if(((IMMessageResponse) result.object).getErrorCode() == 401) {
				
				App.showToast("CmdARecord104中发现需要重新登陆");
				
			}
		}
		ImDataCenter.getInstance().noticeListener(ImDataCenter.SEND_ERROR);
	}

	@Override
	protected String getCommand() {
		return "104";
	}
	
	
}
